package UILayer;

import java.util.Scanner;
import java.lang.Throwable;
import CtrlLayer.*;
import ModelLayer.*;
/**
 * Write a description of class RegisteredCustomerUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RegisteredCustomerUI
{
    private CustomerController customerContainer;
    private ProductController productContainer;
    private SaleController saleContainer;

    public RegisteredCustomerUI()
    {
        customerContainer = new CustomerController();
        productContainer = new ProductController();
        saleContainer = new SaleController();
    }

    public void registeredCustomerMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeRegisteredCustomerMenu();

            switch (choise) 
            {
                case 1: startFindCustomer();
                break;
                case 2: startUpdateCustomer();
                break;
                case 3: startDeleteCustomer();
                break;
                case 4: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writeRegisteredCustomerMenu()
    {  
        int choise = 0;
        try
        {
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Employee Menu ***");
            System.out.println(" *** Speaking with registered customer ***");
            System.out.println(" (1) Find customer and proceed to ordering");
            System.out.println(" (2) Update customer's info");
            System.out.println(" (3) Delete customer");
            System.out.println(" (4) Back");

            choise = keyboard.nextInt();
        }
        catch(Exception e)
        {
            System.out.println("ERROR!");
            registeredCustomerMenuStart();
        }
        return choise;
    }

    private void startFindCustomer()
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the id of the customer:");
       // try
        {
            int id = keyboard.nextInt();
            Customer foundCustomer = customerContainer.findCustomerById(id);
            System.out.println("Name: " + foundCustomer.getName());
            startOrderProduct();    
            
        }

        //catch (Exception e) 
        {
            System.out.println("The customer you are looking for can not be found!");

        }
        pause();
    }

    private void startOrderProduct()
    {
        int index=0;
        boolean exit=false;
        while(exit==false)
        {
            Scanner keyboard = new Scanner(System.in);   
            //try
            {
                System.out.println("Enter sale id: ");
                int saleId = keyboard.nextInt();
                System.out.println("Enter date: ");
                String date = keyboard.nextLine();
                keyboard.nextLine(); //SHE VI EBA MAIKATA I NA SMOTANIQ KOMPILATOR
                Sale sale = new Sale (date, saleId);
                System.out.println("Enter the name of the product you want to buy:");
                String name = keyboard.nextLine();
                Product foundProduct = productContainer.findProductByName(name);
                System.out.println("Barcode: " + foundProduct.getBarCode());
                System.out.println("Product's name: " + foundProduct.getProductName());
                System.out.println("Price: " + foundProduct.getPrice());
                System.out.println("Weight: " + foundProduct.getWeight());
                System.out.println("Location: " + foundProduct.getLocation());
                System.out.println("Quantity available: " + foundProduct.getQuantity());
                System.out.println("============================================");
                System.out.println("How much of this product would you like to buy?");
                int quantity = keyboard.nextInt();
                sale.addSalesLine(new SalesLine (foundProduct, quantity));
                //sale.addProductToList(foundProduct, quantity);
                
//                 if(quantity < foundProduct.getQuantity())
//                 {
//                     foundProduct.setQuantity(foundProduct.getQuantity() - quantity);
//                 }
//                 else
//                 {
//                     throw new Exception();
//                 }
                System.out.println("Would you like us to deliver it?");
                //===========================DA SE DOPISHE TUK=============================================
                System.out.println("Would you like to order more products?");
                System.out.println("       (1) Yes");
                System.out.println("       (2) No");
                int choise = keyboard.nextInt();
                if(choise==1)
                {
                    exit=false;
                }
                else
                {
                    System.out.println("Total price: " + saleContainer.finishSale());
                    exit = true;
                }
            }

            //catch (Exception e) 
            {
                System.out.println("The product you are looking for can not be found or out of stock!");
                exit=true;
            }
            pause();
        }
    }

    private void startUpdateCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        //try
        {
            System.out.println("Enter the ID of the customer:");
            int id = Integer.parseInt(keyboard.nextLine());
            System.out.println("Fill in the new information:");
            System.out.println("Customer's name:");
            String name = keyboard.nextLine();
            System.out.println("Customer's Address:");
            String address = keyboard.nextLine();
            System.out.println("Customer's City:");
            String city = keyboard.nextLine();
            System.out.println("Customer's Post Code:");
            String postCode = keyboard.nextLine();
            System.out.println("Customer's e-mail:");
            String eMail = keyboard.nextLine();

            customerContainer.updateCustomer(id,name,address,city,postCode, eMail);
            //productContainer.updateProduct(barCode,name,price,weight,location, productContainer.findProductByBarCode(barCode).increaseQuantity(quantity));
            System.out.println("The information has been updated!");
        }
        //catch(Exception e) 
        {
            System.out.println("Error! Try again");
        }
        pause();
    }

    private void startDeleteCustomer()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the ID of the customer you want to delete:");
            int id = keyboard.nextInt();
            customerContainer.deleteCustomerById(id);    
        } 
        catch (Exception e) 
        {
            System.out.println("The customer you are looking for can not be found!");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
